package net.pws.oos.web.dto;

import java.util.HashMap;
import java.util.Map;

import net.pws.oos.biz.model.Organization;
import net.pws.oos.biz.model.User;

public class SimpleTreeNode {
	
	private static final String TYPE_USER = "user";
	
	private static final String TYPE_ORGAN = "organ";

	private String id;
	
	private String name;
	
	private String code;
	
	private String type;
	
	private Map<String, String> properties = new HashMap<String, String>();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void addProperty(String key, String value) {
		properties.put(key, value);
	}
	
	public Map<String, String> getProperties() {
		return properties;
	}

	public static SimpleTreeNode from(User user) {
		if (user == null) {
			return null;
		}
		SimpleTreeNode node = new SimpleTreeNode();
		node.setType(TYPE_USER);
		node.setId(user.getId());
		node.setName(user.getName());
		node.setCode(user.getAccount());
		node.addProperty("mobile", user.getMobile());
		return node;
	}
	
	public static SimpleTreeNode from(Organization organ) {
		if(organ == null) {
			return null;
		}
		SimpleTreeNode node = new SimpleTreeNode();
		node.setType(TYPE_ORGAN);
		node.setId(organ.getId());
		node.setName(organ.getShortName());
		node.setCode(organ.getCode());
		node.addProperty("shortName", organ.getShortName());
		node.addProperty("innerCode", organ.getInnerCode());
		node.addProperty("leaf", String.valueOf(organ.isLeaf()));
		return node;
	}
}
